package com.mew.chess.core.game.bjl.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.handler.GameCommonService;
import com.mew.chess.core.common.utils.ChessGameUtil;
import com.mew.chess.core.game.bjl.game.BjlGameManager;
import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.common.BjlLinkType;
import com.mew.chess.core.game.bjl.game.common.BjlPushCMDMapType;
import com.mew.chess.core.game.bjl.game.message.PushDealCardMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

/**发牌阶段
 * 初始化数据
 * @author dylin
 * @create 2021/4/22
 */
@Component
@Slf4j
public class BjlDealCardTask {

    @Autowired
    private GameCommonService gameCommonService;

    public boolean dealCard(int roomId){
        BjlRoomManager roomController = BjlGameManager.getInstance().getController(roomId);
        roomController.getOnlineUser().forEach((k, v) -> {
            if (null == v || v.isWeakClose()){
                roomController.clearOnlineUser(k);
            }
        });

        gameCommonService.checkTMSOnline(roomController.getOnlineUser().values().stream().collect(Collectors.toList()), BjlGameManager.getInstance().getGameId(), roomId);

        if (roomController.getOnlineUser().isEmpty()){
            log.info("Bjl roomId {} not real user is stop ", roomId);
            roomController.stopGame();
            return false;
        }
//
//        TmsServerInfo tmsServerInfo = TmsConfigInfoManager.getInstance().getServerInfo();
//        if (tmsServerInfo.getOnline() < OnlineType.Online.value()){
//            log.info("BJL Start TMS not online {}", tmsServerInfo.toString());
//            roomController.stopGame();
//            roomController.getOnlineUser().forEach((k, v) -> {
//                aucRedisService.offlineSession(k);
//                ChessPushMessageUtil.pushMessage(v, BasisCommandCode.OVERHAULED.cmd(), new EmptyMessage());
//            });
//        }

        roomController.initInfo();
        roomController.setIno(ChessGameUtil.generateINO(roomController.getIno()));
        PushDealCardMessage pushDealCardMessage=roomController.getPushDealCardMessage();
        pushDealCardMessage.setBigPlay(roomController.getBigRList().values());
        pushDealCardMessage.setOnlineNumber(roomController.getPlayerList().size());
        pushDealCardMessage.setIno(roomController.getIno());
        pushDealCardMessage.setCardNumber(roomController.getCardLib().size());
        ChessPushMessageUtil.broadcastMessage(roomController.getOnlineUser().values(), BjlPushCMDMapType.DealCardMessage.code(),pushDealCardMessage);
        roomController.changeLink(BjlLinkType.DealCard.code(),BjlConstantValue.DEAL_CARD_TIME);
        return true;
    }
}
